package com.bjpowernode.web;

import com.bjpowernode.model.TUser;
import com.bjpowernode.query.UserQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/api/owner")
    public R owner() {
        List<TUser> ownerList = userService.getOwnerList();
        return R.OK(ownerList);
    }

    /**
     * 批量删除用户
     * @param ids 用户id字符串,如1,2,3,4
     * @return R
     */
    @DeleteMapping(value = "/api/user")
    public R batchDelUser(@RequestParam(value = "ids") String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = userService.batchDelUserByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * 删除用户
     * @param id 用户id
     * @return R
     */
    @DeleteMapping(value = "/api/user/{id}")
    public R delUser(@PathVariable("id") Integer id) {
        int del = userService.delUserById(id);
        return del >= 1 ? R.OK(): R.FAIL();
    }

    /**
     * 编辑用户信息
     * @param userQuery 前台表单数据(formData类型)
     * @param token 用户信息
     * @return R
     */
    @PutMapping(value = "/api/user")
    public R editUser(UserQuery userQuery, @RequestHeader(value = "Authorization")String token) {
        userQuery.setToken(token);
        int update = userService.updateUser(userQuery);
        return update >= 1 ? R.OK(): R.FAIL();
    }

    /**
     * 新增用户信息
     * @param userQuery 前台表单数据(formData类型)
     * @return R
     */
    @PostMapping(value = "/api/user")
    public R addUser(UserQuery userQuery, @RequestHeader(value = "Authorization")String token) {
        userQuery.setToken(token);
        int save = userService.addUser(userQuery);
        return save >= 1 ? R.OK(): R.FAIL();
    }

    /**
     * 查询用户详情
     * @param id 用户id主键
     * @return tUser
     */
    @GetMapping(value = "/api/user/{id}")
    public R userDetail(@PathVariable("id") Integer id) {
        TUser tUser = userService.getUserById(id);
        return R.OK(tUser);
    }

    /**
     * 用户列表分页查询
     * @param current 当前页
     * @return 用户列表
     */
    @GetMapping(value = "/api/users")
    public R userPage(@RequestParam(value = "current", required = false) Integer current) {
        if (current == null) {
            current = 1;
        }
        PageInfo<TUser> pageInfo = userService.getUserByPage(current);
        return R.OK(pageInfo);
    }

    /**
     * 获取登录人信息
     * @param authentication
     * @return
     */
    @GetMapping(value = "/api/login/info")
    public R loginInfo(Authentication authentication) {
        TUser tUser = (TUser) authentication.getPrincipal();
        return R.OK(tUser);
    }

    /**
     * 免登录
     * @return
     */
    @GetMapping("/api/login/free")
    public R freeLogin() {
        return R.OK();
    }

}
